﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CalculateModelClasses;
using TxRxFileProceed;
using TerFileProceed;
using RayTracingModel;
using FileOutput;

namespace RayTracingProceed
{
    public class RayTracingProceed
    {
        public static void Calculate(string setuppath, string terpath, string txpath, string rxpath)
        {
            string DirectoryPath = GetDirectory(setuppath) + "result\\";
            string ProName = GetProName(setuppath);
            string ter = ReadTer.GetTer(terpath);
            List<Node> txs = TxFileProceed.GetTx(txpath, setuppath, terpath);
            List<List<ReceiveBall>> rxs = RxFileProceed.GetRx(rxpath, setuppath, terpath);
            for (int i = 0; i < txs.Count; i++)
                for (int j = 0; j < rxs.Count; j++)
                {
                    if (rxs[j].Count == 1)
                    {
                        Node tx = new Node(txs[i]);
                        List<List<CalculateModelClasses.Path>> paths = new List<List<CalculateModelClasses.Path>>();
                        //考虑使用多线程获取路径和写路径
                        RayPath.GetTransPath(ter, tx, rxs[j][0], 90);
                        List<CalculateModelClasses.Path> temp = RayPath.GetLegalPaths(tx);
                        if (temp.Count != 0) paths.Add(temp);
                        //写入结果文件
                        P2mFileOutput.p2mfileoutput(paths, DirectoryPath, ProName);
                    }
                    if (rxs[j].Count > 1)
                    {
                        List<List<CalculateModelClasses.Path>> paths = new List<List<CalculateModelClasses.Path>>();
                        for (int k = 0; k < rxs[i].Count; k++)
                        {
                            Node tx = new Node(txs[i]);
                            RayPath.GetTransPath(ter, tx, rxs[j][k], 90);
                            List<CalculateModelClasses.Path> temp = RayPath.GetLegalPaths(tx);
                            if (temp.Count != 0) paths.Add(temp);
                        }
                        //写结果
                        P2mFileOutput.p2mfileoutput(paths, DirectoryPath, ProName);
                    }
                }
        }

        private static string GetDirectory(string path)
        {
            FileInfo file = new FileInfo(path);
            DirectoryInfo dir = file.Directory;
            if (dir.FullName[dir.FullName.Length - 1] != '\\')
                return dir.FullName + "\\";
            else return dir.FullName;
        }

        private static string GetProName(string path)
        {
            FileInfo file = new FileInfo(path);
            string name = file.Name;
            int pos = name.LastIndexOf('.');
            return name.Substring(0, pos);
        }
    }
}
